Debugging

Objectives


Discussion

How to Handle Errors

Back to top


Demonstration

1. Open your unit4 project.  We will demonstrate how to "step through" the program that you created in the last demonstration.  We are using this demonstration just to show how to use the debugging technique and to show you how you can trace through code to better understand what it is doing.  Take your time to experiment and to understand the logic of the code. 

2.  In the menu, choose debug...step into.  Notice what "F" key is used as the shortcut, probably F8 or F11.  In the future you can use this shortcut key.  Notice that as you continue pressing the shortcut key (we'll use F8 for this discussion)  VS steps through the program line by line.  However, it steps right over the function and does not show what is going on inside the function. 

3.  We need to see what is going on in the function SumSeries.   We can make the program step through this function by setting a breakpoint.  You can set a breakpoint by clicking in the margin next to the line of code that you want to break at.  In this case, left click in the margin next to the function header (Private Function SumSeries(...)). When you click in the margin you should see a big dot.  This dot indicates that you have a breakpoint.  Now hit F8 or select debug...Step into.  Continue hitting F8 to step through the program.  At some point (after you type in a number in the textbox and click the button) you should be stepping through the function.  As you step through the function hold the mouse over the different variables. VS will display their current values. 

4.  Experiment with setting breakpoints and stepping through and observing the flow of the program and the values of the variables.  This approach is very simple but you can get alot of information about what your program is doing.  As your programs get more and more complicated this technique should be helpful.

5. We will now experiment with a new function.  Then add the following function.  This function checks a number to determine something about the number. You will have to figure out what it does while you experiment with debugging.

Private Function IsX(ByVal iNum As Int16) As Boolean
   Dim i As Int16
   Dim iRemainder As Int16
   If iNum = 2 Then Return (False)
   For i = 2 To iNum - 1
      iRemainder = iNum Mod i
      If iRemainder = 0 Then Return (False)
   Next
   Return (True)
End Function

 6.  Add a button to your form (btnCheck).  Call the function in the click event for btnCheck.  Pass into the function the value that is in the textbox.  Display the result of the function in a messagebox.   Your click event for btnCheck should look like the following, depending on the name of your textbox.

Private Sub btnPrime_Click(...
   MessageBox.Show(IsX(txtNum.Text))
End Sub

7.  Run the program.  Try to figure out what the function does.  Step through the function and observe the logic and flow.  Check out the values of the variables.  Understand what the function is doing.

Back to top
Exercises

1.  Copy the following function into a project.  Set up a form to use this function.  You need the user to be able to enter a string and True or False.  You also need something to display the result of the function.  When the user clicks the button you should pass the string and True or False into the function and display the result.  Your goal is to use the debugging tools to trace through the function to determine what it does.  Take the time to trace through and observe what is happening. 

Public Function FunctionX(ByVal sString As String, _
ByVal bE As Boolean) As String
   Dim sNew As String
   Dim i, x As Int16
   Dim tTime As Date
   For i = 0 To sString.Length - 1
      If IsNumeric(sString.Chars(i)) Then
         x = CInt(CStr(sString.Chars(i)))
         If (x Mod 2) = 0 Then
            If bE = True Then
               sNew = sNew + sString.Substring(i, 1)
            End If
         Else
            If bE = False Then
               sNew = sNew + sString.Substring(i, 1)
            End If
         End If
     Else
        sNew = sNew + sString.Chars(i)
     End If
     tTime = Now
     Debug.Write(tTime)
   Next
   Return sNew
End Function

  1. Create a table with columns for i, x, and sNew.  Run the program by passing "a1b2c3" and True into the function (however you do) and trace through the function.  Complete the table by adding a row for each value of i as the program runs.  This kind of table can be helpful to determine the logic of complicated loops.
  2. Repeat 'a' but pass False into the function.
  3. What does sString.Chars(i) do?
  4. What does sString.Substring(i,1) do?
  5. What does Debug.Write(tTime) do?
  6. What does the function do?
Back to top

Links & Help
Back to top